package com.nhjf.dao.coursesSeries;

import com.nhjf.model.coursesSeries.CoursesSeries;
import com.nhjf.pojo.qo.coursesSeries.CoursesSeriesQo;

import org.apache.commons.lang.StringUtils;
import org.hibernate.Criteria;
import org.hibernate.FetchMode;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import com.nhjf.dao.BaseDao;
@Repository
public class CoursesSeriesDao extends BaseDao<CoursesSeries, CoursesSeriesQo>{

	@Override
	protected Criteria buildCriteria(Criteria criteria, CoursesSeriesQo qo) {
		// TODO Auto-generated method stub
		criteria.setFetchMode("uploader", qo.isSeachTeacher()?FetchMode.JOIN:FetchMode.SELECT);	
		if(qo!=null) {
			if(qo.getAffiliation()!=null) {
				criteria.add(Restrictions.eq("affiliation",qo.getAffiliation()));
			}
			if(StringUtils.isNotBlank(qo.getCourseSecondaryTitle())) {
				criteria.add(Restrictions.eq("courseSecondaryTitle",qo.getCourseSecondaryTitle()));
			}
			if(StringUtils.isNotBlank(qo.getCover())) {
				criteria.add(Restrictions.eq("cover",qo.getCover()));
			}
			if(StringUtils.isNotBlank(qo.getTotalDetails())) {
				criteria.add(Restrictions.eq("totalDetails",qo.getTotalDetails()));
			}
			if(qo.getUploader()!=null) {
				criteria.add(Restrictions.eq("uploader.id",qo.getUploader()));
			}
			if(StringUtils.isNotBlank(qo.getNickname())) {
				criteria.createAlias("uploader", "uploader");
				criteria.add(Restrictions.like("uploader.nickname",qo.getNickname(),MatchMode.ANYWHERE));
			}
			if(qo.getCourseShare()!=null) {
				criteria.add(Restrictions.eq("courseShare",qo.getCourseShare()));
			}
			if(qo.getCoursesSeriesId()!=null) {
				criteria.add(Restrictions.eq("id",qo.getCoursesSeriesId()));
			}
			if(qo.getCourseTypeOne()!=null) {
				criteria.add(Restrictions.eq("courseTypeOne",qo.getCourseTypeOne()));
			}
			if(qo.getCourseTypeTwo()!=null) {
				criteria.add(Restrictions.eq("courseTypeTwo",qo.getCourseTypeTwo()));
			}
			if(StringUtils.isNotBlank(qo.getCover())) {
				criteria.add(Restrictions.eq("cover",qo.getCover()));
			}
			if(StringUtils.isNotBlank(qo.getTotalLength())) {
				criteria.add(Restrictions.eq("totalLength",qo.getTotalLength()));
			}
			if(StringUtils.isNotBlank(qo.getTotalTitle())) {
				criteria.add(Restrictions.like("totalTitle",qo.getTotalTitle(),MatchMode.ANYWHERE));
			}
			if(qo.getCreationTime()!=null) {
				criteria.add(Restrictions.eq("creationTime",qo.getCreationTime()));
			}
			if(qo.getIsPrice()!=null) {
				criteria.add(Restrictions.eq("isPrice",qo.getIsPrice()));
			}
			if(qo.getPrice()!=null) {
				criteria.add(Restrictions.eq("price",qo.getPrice()));
			}
			if(qo.getIsReview()!=null) {
				criteria.add(Restrictions.eq("isReview",qo.getIsReview()));
			}
			if(qo.getIsSeriesLesson()!=null) {
				criteria.add(Restrictions.eq("isSeriesLesson",qo.getIsSeriesLesson()));
			}
			if(qo.getIsTopping()!=null) {
				criteria.add(Restrictions.eq("isTopping",qo.getIsTopping()));
			}
			if(qo.getPopularity()!=null) {
				criteria.add(Restrictions.eq("popularity",qo.getPopularity()));
			}
			if(qo.getCourseShares()!=null) {
				criteria.add(Restrictions.ne("courseShare",qo.getCourseShares()));
			}
			if(qo.getIsSchooling()!=null) {
				criteria.add(Restrictions.ne("isSchooling",qo.getIsSchooling()));
			}
			if(qo.getIsRelease()!=null) {
				criteria.add(Restrictions.ne("isRelease",qo.getIsRelease()));
			}
			if(qo.getOrganizationId()!=null) {
				criteria.createAlias("uploader", "uploader");
				criteria.createAlias("uploader.traininginstitution", "uploader.traininginstitution");
				criteria.add(Restrictions.eq("uploader.traininginstitution.id",qo.getOrganizationId()));
			}
			
			criteria.addOrder(Order.asc("isTopping"));
			criteria.addOrder(Order.desc("creationTime"));
			
		}
		return criteria;
	}

	@Override
	protected Class<CoursesSeries> getEntityClass() {
		// TODO Auto-generated method stub
		return CoursesSeries.class;
	}

}
